<html>
<head>

 <title>keyvaluestorage</title>

 <script type="text/javascript">

   function KVS_Request(request_document) {
     var request_location = window.location.protocol
                          + '//'+window.location.host
                          + request_document;
     var request_location = 'https://keyvaluestorage.appspot.com'
                          + request_document;
     if (request_location.indexOf('?')>0) {
       request_location += '&random=' + Math.random();
     }
     var tag_script = document.createElement('script');
     var attr_src = document.createAttribute('src');
         attr_src.nodeValue = request_location;
     tag_script.setAttributeNode(attr_src);
     if (confirm('send request: '+request_location)) {
       var html_body = document.getElementsByTagName("body")[0];
           html_body.appendChild(tag_script);
     }
   } 

   function KVS_UserId() {
     var redirect_location = escape('/keyvaluestorage/api/v1/redirect/?redirect=')
                            + escape(window.location.href);
     KVS_Request('/keyvaluestorage/api/v1/userid/?callback=KVS_onUserId&redirect='+redirect_location);
     return false;
   } 

   function KVS_onUserId(user) {
     if (user.STATUS == 'login') {
       alert(
         'UserID: '+user.USERID+"\n"+
         'Nickname: '+user.NICKNAME+"\n"+
         'LogoutURL: '+user.LOGOUTURL+"\n"+
         ''
       );
       if (confirm('logout?')) {
         location.href=user.LOGOUTURL;
       }
     } 
     else if (user.STATUS == 'logout') {
       alert('LoginURL: '+user.LOGINURL);
       location.href=user.LOGINURL;
     }
   }

   function KVS_ItemSet() {
     var item_key = escape(document.forms['itemset']['key'].value);
     var item_value = escape(escape(document.forms['itemset']['value'].value));
     KVS_Request('/keyvaluestorage/api/v1/itemset/?callback=KVS_onItemSet&key='+item_key+'&value='+item_value);
     return false;
   } 

   function KVS_onItemSet(item) {
     alert(item.STATUS+' '+item.KEY);
   }

   function KVS_ItemGet() {
     var item_key = escape(document.forms['itemget']['key'].value);
     KVS_Request('/keyvaluestorage/api/v1/itemget/?callback=KVS_onItemGet&key='+item_key);
     return false;
   } 

   function KVS_onItemGet(item) {
     alert(item.STATUS+' '+item.KEY+' '+item.INFO);
     if (item.STATUS == 'login') {
       document.forms['itemget']['value'].value = unescape(item.VALUE);
     }
   }

   function KVS_ItemDel() {
     var item_key = escape(document.forms['itemdel']['key'].value);
     KVS_Request('/keyvaluestorage/api/v1/itemdel/?callback=KVS_onItemDel&key='+item_key);
     return false;
   } 

   function KVS_onItemDel(item) {
     alert(item.STATUS+' '+item.KEY);
   }

 </script>

</head>
<body>

 <h1>keyvaluestorage</h1>

 <hr/>

 <h2>/api/v1/redirect/</h2>

 <pre>

 ### REQUEST ###

 /keyvaluestorage/api/v1/redirect/
  ?redirect=http://www.example.com/#onlogin

 </pre>

 <a href="https://keyvaluestorage.appspot.com/keyvaluestorage/api/v1/redirect/?redirect=http://www.google.com" target="_blank">redirect</a>
 <br/>

 <hr/>

 <h2>/api/v1/userid/</h2>

 <pre>

 ### REQUEST ###

 /keyvaluestorage/api/v1/userid/
  ?callback=JavaScriptFunctionName
  &redirect=/keyvaluestorage/

 ### RESPONCE ###

 {'STATUS':'logout',
  'LOGINURL':'http://...'}

 {'STATUS':'login',
  'LOGOUTURL':'http://...',
  'USERID':'abc@xyz.com'}

 </pre>

 <a href="#" onclick="return KVS_UserId()">login</a>
 <br/>


 <hr/>

 <h2>/api/v1/itemset/</h2>

 <pre>

 ### REQUEST ###

 /keyvaluestorage/api/v1/itemset/
  ?callback=JavaScriptFunctionName
  &key=datakey
  &value=datavalue

 ### RESPONCE ###

 {'STATUS':'logout',
  'KEY':'datakey'}

 {'STATUS':'login',
  'KEY':'datakey'}

 </pre>

 <form name="itemset">
  key:<br/>
  <input name="key" type="text" value="abc" readonly="readonly" />
  <br/>
  value:<br/>
  <input name="value" type="text" value="123" />
  <br/>
 </form>

 <a href="#" onclick="return KVS_ItemSet()">save</a>
 <br/>

 <hr/>

 <h2>/api/v1/itemget/</h2>

 <pre>

 ### REQUEST ###

 /keyvaluestorage/api/v1/itemget/
  ?callback=JavaScriptFunctionName
  &key=datakey

 ### RESPONCE ###

 {'STATUS':'logout',
  'KEY':'datakey'}

 {'STATUS':'login',
  'INFO':'NOTFOUND',
  'KEY':'datakey',
  'VALUE':''}

 {'STATUS':'login',
  'INFO':'FOUND(DS)',
  'KEY':'datakey',
  'VALUE':'datavalue'}

 {'STATUS':'login',
  'INFO':'FOUND(MC)',
  'KEY':'datakey',
  'VALUE':'datavalue'}

 </pre>

 <form name="itemget">
  key:<br/>
  <input name="key" type="text" value="abc" readonly="readonly" />
  <br/>
  value:<br/>
  <input name="value" type="text" value="" readonly="readonly" />
  <br/>
 </form>

 <a href="#" onclick="return KVS_ItemGet()">load</a>
 <br/>

 <hr/>

 <h2>/api/v1/itemdel/</h2>


 <pre>

 ### REQUEST ###

 /keyvaluestorage/api/v1/itemdel/
  ?callback=JavaScriptFunctionName
  &key=datakey

 ### RESPONCE ###

 {'STATUS':'logout',
  'KEY':'datakey'}

 {'STATUS':'login',
  'INFO':'NOTFOUND',
  'KEY':'datakey'}

 {'STATUS':'login',
  'INFO':'FOUND',
  'KEY':'datakey'}

 </pre>

 <form name="itemdel">
  key:<br/>
  <input name="key" type="text" value="abc" readonly="readonly" />
  <br/>
 </form>

 <a href="#" onclick="return KVS_ItemDel()">free</a>
 <br/>

 </body>
</html>